package com.cad3d.filemanager.action;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.cad3d.filemanager.dao.Filemanagement;
import com.cad3d.filemanager.dbutils.FileManagementTableTool;
import com.opensymphony.xwork2.ActionSupport;

public class EditFileAction extends ActionSupport implements
		ServletRequestAware {

	private String oper;
//	private String fileName;
	private String thumbnailPath;
//	private String absolutePath;
	private String filePublic;
	private String id;
	private HttpServletRequest servletRequest;
	
	private Logger log = Logger.getLogger(EditFileAction.class);

	public String execute() {
		try {
					
			if (oper.equalsIgnoreCase("edit")) {
				Filemanagement file = FileManagementTableTool.getInstance()
						.findFileById(Integer.parseInt(id));
//				file.setFileName(fileName);
				
				file.setThumbnailPath(thumbnailPath);
		//		file.setAbsolutePath(absolutePath);
				
				log.debug("Public: "+filePublic);
				log.debug("so sanh voi on: "+filePublic.equalsIgnoreCase("on"));
				
				if(filePublic.equalsIgnoreCase("on")||filePublic.equalsIgnoreCase("true") ) {
					file.setFilePublic(true);
				}else {
					file.setFilePublic(false);
				}
			//	file.setFilePublic(filePublic);
				
				FileManagementTableTool.getInstance().editFileInfo(file);
			} else if (oper.equalsIgnoreCase("del")) {
				String[] idArr = id.split(",");
				for (String stringId : idArr) {
					
					String serverFilePath = servletRequest.getSession().getServletContext()
								.getRealPath("/");
					Filemanagement file = FileManagementTableTool.getInstance()
							.findFileById(Integer.parseInt(stringId));
					String delPath = serverFilePath + file.getFilePath();
					log.debug("Delete file: " + delPath);
					
					File delFile = new File(delPath);
					if(delFile.exists()){
						log.debug("Delete success");
						delFile.delete();
					}else{
						log.debug("File not exits");
					}

					FileManagementTableTool.getInstance().deleteFile(file);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return "error";
		}
		return "success";
	}


	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public String getThumbnailPath() {
		return thumbnailPath;
	}

	public void setThumbnailPath(String thumbnailPath) {
		this.thumbnailPath = thumbnailPath;
	}

//	public String getAbsolutePath() {
//		return absolutePath;
//	}
//
//	public void setAbsolutePath(String absolutePath) {
//		this.absolutePath = absolutePath;
//	}

//	public String getFileName() {
//		return fileName;
//	}
//
//	public void setFileName(String fileName) {
//		this.fileName = fileName;
//	}
	
	public String getId() {
		return id;
	}

	public String isFilePublic() {
		return filePublic;
	}


	public void setFilePublic(String filePublic) {
		this.filePublic = filePublic;
	}


	public Logger getLog() {
		return log;
	}


	public void setLog(Logger log) {
		this.log = log;
	}


	public void setId(String id) {
		this.id = id;
	}

	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;

	}

}
